package com.xfhy.wallpaperdemo.lock;

import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import com.xfhy.wallpaperdemo.MyApplication;

import java.io.Serializable;

import androidx.core.app.NotificationManagerCompat;

public class NotifyService extends Service implements Serializable {
    public static final String ACTION_CLOSE_FLASHLIGHT = "action.CLOSE_FLASHLIGHT";
    public static final String ACTION_OPEN_FLASHLIGHT = "action.OPEN_FLASHLIGHT";
    public static final String ACTION_SHOW_NORMAL_NOTI = "action.SHOW_NORMAL_NOTI";
    public static final String ACTION_SHOW_PIC_NOTI = "action.SHOW_PIC_NOTI";
    public static final String TAG = "NotifyService";

    private void sendClickFlashlight() {
        Log.d(TAG, "sendClickFlashlight");
    }

    private void showNormalNotification() {
        Notification normalPermanentNotifi;
        normalPermanentNotifi = NotifyUtil.getNormalPermanentNotifi("优化", "优化222");
        startForeground(102, normalPermanentNotifi);
        NotificationManagerCompat.from(MyApplication.sContext).notify(102, normalPermanentNotifi);
    }

    private void showPicNotification(String str) {
        /*Notification picPermanentNotifi = NotifyUtil.getPicPermanentNotifi(bl.newInstance(MyApplication.sContext).getFromCacheOrLocalPublic(str));
        if (picPermanentNotifi != null) {
            startForeground(102, picPermanentNotifi);
            NotificationManagerCompat.from(MyApplication.sContext).notify(102, picPermanentNotifi);
        }*/
    }

    private void showToolbarNotification() {
        Notification permanentNotification = NotifyUtil.getPermanentNotification();
        startForeground(102, permanentNotification);
        NotificationManagerCompat.from(MyApplication.sContext).notify(102, permanentNotification);
    }

    public static void startNormalNoti(Context context) {
        Intent intent = new Intent(context, NotifyService.class);
        intent.setAction(ACTION_SHOW_NORMAL_NOTI);
        if (Build.VERSION.SDK_INT >= 26) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
    }

    public static void startPicNoti(Context context, String str) {
        Intent intent = new Intent(MyApplication.sContext, NotifyService.class);
        intent.setAction(ACTION_SHOW_PIC_NOTI);
        intent.putExtra("pic_url", str);
        if (Build.VERSION.SDK_INT >= 26) {
            MyApplication.sContext.startForegroundService(intent);
        } else {
            MyApplication.sContext.startService(intent);
        }
    }

    public static void startToolNoti(Context context) {
        Intent intent = new Intent(context, NotifyService.class);
        if (Build.VERSION.SDK_INT >= 26) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int i, int i2) {
        MyApplication.initLock();
        Log.e(TAG, "onStartCommand");
        if (intent != null && ACTION_SHOW_NORMAL_NOTI.equals(intent.getAction())) {
            Log.e(TAG, "ACTION_SHOW_NORMAL_NOTI");
            showNormalNotification();
            return super.onStartCommand(intent, i, i2);
        } else if (intent == null || !ACTION_SHOW_PIC_NOTI.equals(intent.getAction()) || TextUtils.isEmpty(intent.getStringExtra("pic_url"))) {
            boolean z2 = true;
            if (intent != null && ACTION_OPEN_FLASHLIGHT.equals(intent.getAction())) {
                sendClickFlashlight();
            }
            if (intent != null && ACTION_CLOSE_FLASHLIGHT.equals(intent.getAction())) {
                sendClickFlashlight();
            }
            if (z2) {
                showToolbarNotification();
            }
            return super.onStartCommand(intent, i, i2);
        } else {
            Log.e(TAG, "ACTION_SHOW_PIC_NOTI");
            showPicNotification(intent.getStringExtra("pic_url"));
            return super.onStartCommand(intent, i, i2);
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public void unbindService(ServiceConnection serviceConnection) {
        super.unbindService(serviceConnection);
    }
}
